﻿using System.Security.Principal;
using System.Web;
using Microsoft.Practices.Unity;
using Netkk.Common.Interfaces.Services;
using Netkk.Common.Models;

namespace Netkk.Core.Auth
{
    public class CustomIdentity : IIdentity
    {
        private User user;

        public IIdentity ParentIdentity { get; private set; }

        public string AuthenticationType { get; private set; }

        public string Name { get; private set; }

        private IUserService UserService
        {
            get
            {
                IUnityContainer container = (IUnityContainer)HttpContext.Current.Application["container"];
                return container.Resolve<IUserService>();
            }
        }

        public bool IsAuthenticated
        {
            get { return User != null && !HttpContext.Current.User.IsInRole("Guest"); }
        }

        public User User
        {
            get
            {
                if (user == null)
                {
                    user = UserService.GetUserByLoginName(Name);
                }
                return user;
            }
        }

        public CustomIdentity(IIdentity identity)
        {
            ParentIdentity = identity;
            AuthenticationType = identity.AuthenticationType;
            Name = identity.Name;
        }
        public CustomIdentity(string name, string authType)
        {
            AuthenticationType = authType;
            Name = name;
        }

    }
}